import requests
import json
import os
import sys
import pandas as pd


def load_test_data():
    file_path = './data/cargo_name_identify_old.txt'
    data = set()

    with open(file_path,'r',encoding='utf-8') as fread:
        for index,line in enumerate(fread):
            if 'input_sentence:' in line:
                if 'cargoId' in line:
                    continue
                try:
                    input_json = json.loads(line.strip().replace("input_sentence: ","").replace("'","\""))
                    if 'cargoName' in input_json:
                        data.add(input_json['cargoName'].replace("\n",""))

                    if len(data)> 20000:
                        break
                except Exception as e:
                    continue

    for i in data:
        if '食口' in i:
            j= 1
    return list(data)


url = "http://dev-beetle.amh-group.com/beetle/cargo_name/cargo_name_identify"
def get_new_result(cargo_name,url):

    post_body = {
        "cargoName":cargo_name,
        "Transfer":1
    }
    headers = {
        'Content-Type':"application/json"
    }

    req = requests.post(url,data=json.dumps(post_body),headers=headers)
    return req.json()

def check_same(new_result,old_result):
    check_key = ["cargoName",'type']

    save_list = []

    same = True

    diff_value = 0
    diff_type = 0
    null_new = 0
    for key in check_key:
        old_value = old_result[key]
        if new_result[key] != old_value:
            same = False
            if key == "cargoName":
                if old_value == "":
                    null_new = 1
                diff_value = 1
            else:
                if new_result[key] in [ "2","5"] and old_value in ['2','5']:
                    same = True
                else:
                    diff_type = 1
            break

    if not same:
        save_list.append(old_result['cargoName'])
        save_list.append(old_result['type'])
        save_list.append(new_result['cargoName'])
        save_list.append(new_result['type'])
        save_list.append(diff_value)
        save_list.append(diff_type)
        save_list.append(null_new)


    return same,save_list

import time
def make_old_json(data_list):

    typeCount = {}
    url = 'http://127.0.0.1:8088/cangjie_gateway/cargo_name_identify'
    if not os.path.exists('./result/cargo_name_identify/old_json.txt'):
        with open('./result/cargo_name_identify/old_json.txt','w',encoding='utf-8') as fwrite:
            for name in data_list:
                result = get_new_result(name,url)
                time.sleep(0.01)
                fwrite.write(name+"$$"+json.dumps(result,ensure_ascii=False)+"\n")

                if  result and "cargoNameInfo" in result['data'] and 'type' in result['data']["cargoNameInfo"]:
                    pass
                else:
                    print(name+":error")



def make_new_json(data_list):
    url = 'http://127.0.0.1:8080/beetle/cargo_name/cargo_name_identify'
    if not os.path.exists('./result/cargo_name_identify/new_json.txt'):
        with open('./result/cargo_name_identify/new_json.txt','w',encoding='utf-8') as fwrite:
            for name in data_list:
                time.sleep(0.001)
                result = get_new_result(name,url)
                fwrite.write(name+"$$"+json.dumps(result,ensure_ascii=False)+"\n")
                if result and 'data'in result and "cargoNameInfo" in result['data'] and 'type' in result['data']["cargoNameInfo"]:
                    pass
                else:
                    print(name + ":error")



if __name__ == '__main__':
    old_data_list = load_test_data()
    old_data_list = list(set(old_data_list))
    make_old_json(old_data_list)
    make_new_json(old_data_list)


    total = len(old_data_list)


    result_save_path = './result/cargo_name_identify'
    same_file_path = os.path.join(result_save_path,'same.txt')
    diff_file_path = os.path.join(result_save_path,'diff.csv')

    same_writer = open(same_file_path,'w',encoding='utf-8')
    # same_num = 0

    old_list = {}
    new_list = {}
    name_list = old_data_list
    with open('./result/cargo_name_identify/old_json.txt','r',encoding='utf-8') as fread:
        for line in fread:
            try:
                token,json_str = line.strip().split("$$")
                old_list[token] = json.loads(json_str)
            except Exception as e:
                j = 1

    with open('./result/cargo_name_identify/new_json.txt','r',encoding='utf-8') as fread:
        for line in fread:
            try:
                token, json_str = line.strip().split("$$")
                new_list[token] = json.loads(json_str)
            except Exception as e:
                j = 1

    same_num  = 0

    df = pd.DataFrame(columns=["原始文本","老_cargoname","老_type","新_cargoname","新_type","货名解析不同","type解析不同","老接口未解析出内容"])
    df_line = 0
    for cargo_name in name_list:
        if cargo_name not in new_list or cargo_name not in old_list:
            continue
        same,save_list = check_same(new_list[cargo_name]['data']['cargoNameInfo'],old_list[cargo_name]['data']['cargoNameInfo'])
        if same:
            same_num += 1
            same_writer.write("%s:%s\n" % (cargo_name,json.dumps(new_list[cargo_name])))
        else:
            save_str = '%s: old_result:%s    new_result:%s\n' % (cargo_name,json.dumps(old_list[cargo_name]['data']['cargoNameInfo'],ensure_ascii=False),json.dumps(new_list[cargo_name]['data']['cargoNameInfo'],ensure_ascii=False))
            #print(save_str)
            save_list.insert(0,cargo_name)
            df.loc[df_line] = save_list
            df_line += 1


    print(df.head())
    df.to_csv(diff_file_path)
    same_writer.close()
    print('总共测试数据为%s条，相同结果的数据为%s' % (total,same_num))












